package com.ce.pms.occupation.service.impl;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IMeterService;
import com.ce.pms.house.vo.MeterVO;
import com.ce.pms.occupation.dao.IIdentityValidateDao;
import com.ce.pms.occupation.dao.IInspecteRequisiteDao;
import com.ce.pms.occupation.dao.IPerfectItemRequisitionDao;
import com.ce.pms.occupation.model.IdentityValidateDO;
import com.ce.pms.occupation.model.InspecteRequisiteDO;
import com.ce.pms.occupation.model.MeterCardinalNumberDO;
import com.ce.pms.occupation.model.PerfectItemDispatchDO;
import com.ce.pms.occupation.model.PerfectItemRequisitionDO;
import com.ce.pms.occupation.service.IIdentityValidateService;
import com.ce.pms.occupation.service.IInspecteRequisiteService;
import com.ce.pms.occupation.vo.InspecteRequisiteVO;
import com.ce.pms.occupation.vo.MeterCardinalNumberVO;
import com.ce.pms.occupation.vo.PerfectItemRequisitionVO;
import com.ce.pms.system.occupation.model.PerfectItemTypeConfigDO;
import com.ce.util.BeanUtil;
import com.sitechasia.webx.core.utils.base.PopulateUtil;


/**
 * 验房登记Service实现
 * @author 王浩羽
 *
 */
public class InspecteRequisiteServiceImpl extends PmsBaseServiceImpl implements IInspecteRequisiteService
{

	private IInspecteRequisiteDao inspecteRequisiteDao;
	private IIdentityValidateService identityValidateService;
	private IIdentityValidateDao identityValidateDao;
	private IPerfectItemRequisitionDao perfectItemRequisitionDao;
	private IMeterService meterService;

	public void setMeterService(IMeterService meterService)
	{
		this.meterService = meterService;
	}

	public void setPerfectItemRequisitionDao(IPerfectItemRequisitionDao perfectItemRequisitionDao)
	{
		this.perfectItemRequisitionDao = perfectItemRequisitionDao;
	}


	public void setIdentityValidateDao(IIdentityValidateDao identityValidateDao)
	{
		this.identityValidateDao = identityValidateDao;
	}


	public void setIdentityValidateService(IIdentityValidateService identityValidateService)
	{
		this.identityValidateService = identityValidateService;
	}


	public void setInspecteRequisiteDao(IInspecteRequisiteDao inspecteRequisiteDao)
	{
		this.inspecteRequisiteDao = inspecteRequisiteDao;
	}


	public InspecteRequisiteVO addInspecteRequisite(InspecteRequisiteVO inspecteRequisiteVO)
	{
		if(!isAllowAddInspecteRequisite(inspecteRequisiteVO.getIdentityValidate().getKeyword()))
		{
			throw new BusinessException("occupation:occupation_inspecteRequisite_add_exception",
										BusinessException.WARNING);
		}
		InspecteRequisiteDO inspecteRequisiteDO = new InspecteRequisiteDO();
		inspecteRequisiteDO = assembleInspecteRequisiteV2D(inspecteRequisiteVO, inspecteRequisiteDO,"add");
		inspecteRequisiteDao.saveOrUpdateObject(inspecteRequisiteDO);
		Long identityValidateId = inspecteRequisiteVO.getIdentityValidate().getKeyword();
		//通知验房
		if(inspecteRequisiteVO.getCheckResult().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO))
		{//不通过
			identityValidateService.receiveAddedOccInspectionRequisition(identityValidateId, SysrefcodeDO.INSPECT_RESULT_NOT_PASS);
		}
		if(inspecteRequisiteVO.getCheckResult().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE))
		{//通过
			identityValidateService.receiveAddedOccInspectionRequisition(identityValidateId, SysrefcodeDO.INSPECT_RESULT_PASS);
			//把走表数据写入
			Set<MeterVO> meters = fillMeterDataVO(inspecteRequisiteVO);
			for(MeterVO meterVO : meters)
			{
				meterService.editMeterRadix(meterVO);
			}
		}
		return inspecteRequisiteVO;
	}

	/**
	 * 功能描述: 填充走表数据 <br>
	 * @param inspecteRequisiteVO
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 16, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Set<MeterVO> fillMeterDataVO(InspecteRequisiteVO inspecteRequisiteVO){
		Set<MeterCardinalNumberVO> meterCardinalNumbers = inspecteRequisiteVO.getMeterCardinalNumbers();
		Set<MeterVO> meters = new HashSet<MeterVO>();
		for(MeterCardinalNumberVO meterCardinalNumberVO : meterCardinalNumbers)
		{
			if(meterCardinalNumberVO.getWaterMeterId()!= null && meterCardinalNumberVO.getWaterMeter()!=null)
			{
				//001 水表
				MeterVO water = new MeterVO();
				water.setKeyword(meterCardinalNumberVO.getWaterMeterId());
				water.setHouseId(meterCardinalNumberVO.getHouseId());
				water.setMeterTypeCode(SysrefcodeDO.HM_WATER_METER_TYPE_CODE);
				water.setBase(meterCardinalNumberVO.getWaterMeter());
				water.setModifier(meterCardinalNumberVO.getModifier());
				meters.add(water);
			}

			if(meterCardinalNumberVO.getHouseId()!=null && meterCardinalNumberVO.getAmmeter()!=null)
			{
				//002电表
				MeterVO electricity = new MeterVO();
				electricity.setKeyword(meterCardinalNumberVO.getAmmeterId());
				electricity.setHouseId(meterCardinalNumberVO.getHouseId());
				electricity.setMeterTypeCode(SysrefcodeDO.HM_AMMETER_METER_TYPE_CODE);
				electricity.setBase(meterCardinalNumberVO.getAmmeter());
				electricity.setModifier(meterCardinalNumberVO.getModifier());
				meters.add(electricity);
			}
		}
		return meters;
	}

	public void deleteInspecteRequisite(Long inspecteRequisiteId)
	{
		if(!isAllowDeleteInspecteRequisite(inspecteRequisiteId))
		{
			throw new BusinessException("occupation:occupation_inspecteRequisite_delete_exception",
										BusinessException.WARNING);
		}
		inspecteRequisiteDao.deleteObjectById(inspecteRequisiteId);
	}


	public InspecteRequisiteVO editInspecteRequisite(InspecteRequisiteVO inspecteRequisiteVO)
	{
		InspecteRequisiteDO inspecteRequisiteDO = isAllowEditInspecteRequisite(inspecteRequisiteVO.getKeyword());
		if(inspecteRequisiteDO==null)
		{
			throw new BusinessException("occupation:occupation_inspecteRequisite_edit_exception",
										BusinessException.WARNING);
		}
		
//		InspecteRequisiteDO inspecteRequisiteDO = inspecteRequisiteDao.findById(inspecteRequisiteVO.getKeyword());
		inspecteRequisiteDO = assembleInspecteRequisiteV2D(inspecteRequisiteVO, inspecteRequisiteDO,"edit");
		Long identityValidateId = inspecteRequisiteDO.getIdentityValidate().getKeyword();
		Long lastestInspecteRequisiteId = inspecteRequisiteDao.getTheLastestInspecteRequisiteId(identityValidateId);
		if(lastestInspecteRequisiteId!=null)
		{
			if(inspecteRequisiteVO.getKeyword().longValue()!=lastestInspecteRequisiteId.longValue())
			{
				throw new BusinessException("occupation:occupation_inspecteRequisite_edit_exception",
											BusinessException.WARNING);
			}
		}
		inspecteRequisiteDao.updateObject(inspecteRequisiteDO);
		
		//通知验房
		if(inspecteRequisiteVO.getCheckResult().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO))
		{//不通过
			identityValidateService.receiveAddedOccInspectionRequisition(identityValidateId, SysrefcodeDO.INSPECT_RESULT_NOT_PASS);
		}
		if(inspecteRequisiteVO.getCheckResult().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE))
		{//通过
			identityValidateService.receiveAddedOccInspectionRequisition(identityValidateId, SysrefcodeDO.INSPECT_RESULT_PASS);
			//把走表数据写入
			Set<MeterVO> meters = fillMeterDataVO(inspecteRequisiteVO);
			for(MeterVO meterVO : meters)
			{
				meterService.editMeterRadix(meterVO);
			}
		}
		return inspecteRequisiteVO;
	}


	public InspecteRequisiteVO getInspectRequisitionDetail(Long inspecteRequisiteId)
	{
		InspecteRequisiteDO inspecteRequisiteDO = inspecteRequisiteDao.findById(inspecteRequisiteId);
		if(inspecteRequisiteDO==null)
		{
			return null;
		}
		InspecteRequisiteVO inspecteRequisiteVO = new InspecteRequisiteVO();
		return assembleInspecteRequisiteD2V(inspecteRequisiteDO, inspecteRequisiteVO);
	}


	/**
	 * 功能描述:检查是否可以进行添加验房登记 <br>
	 * 1、身份验证已经通过<br>
	 * 2、针对本房号（或管理房号），验房登记未通过。<br>
	 * @param identityValidateId
	 * @return Create author: 王浩羽 <br>
	 *         Create on:Jan 5, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	public boolean isAllowAddInspecteRequisite(Long identityValidateId)
	{
		if(identityValidateId==null)
		{
			return false;
		}
		IdentityValidateDO identityValidateDO = identityValidateDao.findById(identityValidateId);
		if(identityValidateDO == null)
		{
			return false;
		}
		if(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(identityValidateDO.getIdentityState()))
		{ //验证未通过
			return false;
		}
		if(SysrefcodeDO.INSPECT_RESULT_PASS.equals(identityValidateDO.getInspectResult()) )
		{//验房已经通过
			return false;
		}
		return true;
	}


	/**
	 * 功能描述: 是否已经存在验房通过记录 <br>
	 * 前提：身份验证已经通过，但是钥匙移交没进行。
	 * @param identityValidateDO
	 * @return Create author: 王浩羽 <br>
	 *         Create on:Jan 9, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private InspecteRequisiteVO findExistInspecteRequisiteOK(IdentityValidateDO identityValidateDO)
	{
		Long houseId = identityValidateDO.getHouse().getKeyword();
		List<InspecteRequisiteVO> inspecteVOs = queryInspecteRequisiteByHouseId(houseId);
		for(InspecteRequisiteVO inspecteRequisiteVO : inspecteVOs)
		{
			if(inspecteRequisiteVO.getCheckResult().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE))
			{ // 如果已经验房通过，
				return inspecteRequisiteVO;
			}
		}
		return null;
	}


	public List<InspecteRequisiteVO> queryInspecteRequisiteByHouseId(Long houseId)
	{
		List<InspecteRequisiteDO> inspecteDOs = inspecteRequisiteDao.queryInspecteRequisiteByHouseId(houseId);
		List<InspecteRequisiteVO> inspecteVOs = new ArrayList<InspecteRequisiteVO>();
		if(inspecteDOs != null && !inspecteDOs.isEmpty())
		{
			for(InspecteRequisiteDO inspecteDO : inspecteDOs)
			{
				InspecteRequisiteVO inspecteVO = new InspecteRequisiteVO();
				inspecteVO = assembleInspecteRequisiteD2V(inspecteDO, inspecteVO);
				inspecteVOs.add(inspecteVO);
			}
		}
		return inspecteVOs;
	}


	/**
	 * 功能描述: 检查是否可以进行修改验房登记 <br>
	 * 1、针对本房号，身份验证通过<br>
	 * 2、针对本房号，通过的验房未通过。<br>
	 * @param inspecteRequisite
	 * @return 返回null时不允许修改
	 * Create author: 王浩羽 <br>
	 *         Create on:Jan 5, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	public InspecteRequisiteDO isAllowEditInspecteRequisite(Long inspecteRequisite)
	{
		InspecteRequisiteDO inspecteDO = inspecteRequisiteDao.findById(inspecteRequisite);
		if(inspecteDO == null)
		{
			return null;
		}
		InspecteRequisiteVO passInspecte = findExistInspecteRequisiteOK(inspecteDO.getIdentityValidate());
		//如果存在验房通过记录，但是不是修改记录本身，则不能再修改
		if(passInspecte!=null && passInspecte.getKeyword().longValue()!=inspecteRequisite.longValue())
		{
			//throw new BusinessException("occupation:occupation_inspecteRequisite_edit_exception",BusinessException.WARNING);
			return null;
		}
		if(isExistPerfectItemDealingOrOver(inspecteRequisite))
		{
			return null;
		}
		return inspecteDO;
	}

	/**
	 * 功能描述: 是否存在的整改项目如果已经整改即派工或完工 <br>
	 * @param identityValidate
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 9, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private boolean isExistPerfectItemDealingOrOver(Long inspecteRequisiteId)
	{
		List<PerfectItemRequisitionDO> list = perfectItemRequisitionDao.queryPerItemListByInspReqId(inspecteRequisiteId);
		if(list==null || list.isEmpty())
		{
			return false;
		}
		for(PerfectItemRequisitionDO perfectItemRequisitionDO : list)
		{
			//该项目是否完工 1 : 完工 0 : 未完工
			if(perfectItemRequisitionDO.getCompleteYn().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE))
			{
				return true;
			}
			//是否已派工 1 ：已派工 0 ：未派工
			if(perfectItemRequisitionDO.getDispatchYn().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE))
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * 功能描述: 检查是否可以删除验房登记 <br>
	 * 1、针对所做验房登记记录删除，对于已经存在派工或完工的项目不得删除。<br>
	 * 2、如果存在验房通过登记，则不可删除。<br>
	 * @param inspecteRequisiteId
	 * @return Create author: 王浩羽 <br>
	 *         Create on:Jan 5, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	public boolean isAllowDeleteInspecteRequisite(Long inspecteRequisiteId)
	{
		InspecteRequisiteDO inspecteDO = inspecteRequisiteDao.findById(inspecteRequisiteId);
		if(inspecteDO == null)
		{
			return false;
		}
		if(inspecteDO.getCheckResult().equals(SysrefcodeDO.CONSTANTS_DIGITAL_ONE))
		{
			return false;
		}
		if(isExistPerfectItemDealingOrOver(inspecteRequisiteId))
		{
			return false;
		}
		if(findExistInspecteRequisiteOK(inspecteDO.getIdentityValidate())!=null)
		{
			return false;
		}
		return true;
	}


	/**
	 * 功能描述: VO 转 DO <br>
	 * @param inspecteRequisiteVO
	 * @param inspecteRequisiteDO
	 * @param operation
	 * @return Create author: 王浩羽 <br>
	 *         Create on:Jan 5, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private InspecteRequisiteDO assembleInspecteRequisiteV2D(InspecteRequisiteVO inspecteRequisiteVO,
																InspecteRequisiteDO inspecteRequisiteDO, String operation)
	{
		BeanUtil.convertVoAsDo(inspecteRequisiteVO, inspecteRequisiteDO);
		Long identityValidateId = inspecteRequisiteVO.getIdentityValidateId();
		if(identityValidateId==null && inspecteRequisiteVO.getIdentityValidate()!=null)
		{
			identityValidateId = inspecteRequisiteVO.getIdentityValidate().getKeyword();
		}
		IdentityValidateDO identityValidateDO = identityValidateDao.findById(identityValidateId);
		inspecteRequisiteDO.setIdentityValidate(identityValidateDO);
		Set<MeterCardinalNumberVO> meterCardinalNumberVOSet = inspecteRequisiteVO.getMeterCardinalNumbers();
		inspecteRequisiteDO.getMeterCardinalNumbers().clear();
		for(MeterCardinalNumberVO meterCardinalNumberVO : meterCardinalNumberVOSet)
		{
			MeterCardinalNumberDO meterCardinalNumberDO = new MeterCardinalNumberDO();
			meterCardinalNumberDO = assembleMeterCardinalNumberV2D(meterCardinalNumberVO, meterCardinalNumberDO,
																	inspecteRequisiteDO);
			inspecteRequisiteDO.getMeterCardinalNumbers().add(meterCardinalNumberDO);
		}
		Set<PerfectItemRequisitionDO> perfectItemRequisitions  = inspecteRequisiteDO.getPerfectItemRequisitions();

		Set<PerfectItemRequisitionVO> perfectItemRequisitionVOSet = inspecteRequisiteVO.getPerfectItemRequisitions();
		if (operation.equals("add")) {
			for(PerfectItemRequisitionVO perfectItemRequisitionVO : perfectItemRequisitionVOSet){
				PerfectItemRequisitionDO perfectItemRequisitionDO = new PerfectItemRequisitionDO();
				perfectItemRequisitionDO = assemblePerfectItemRequisitionV2D(perfectItemRequisitionVO, perfectItemRequisitionDO,inspecteRequisiteDO);
				inspecteRequisiteDO.getPerfectItemRequisitions().add(perfectItemRequisitionDO);
			}
		}else if(operation.equals("edit")){
			for(PerfectItemRequisitionVO perfectItemRequisitionVO : perfectItemRequisitionVOSet)
			{
				PerfectItemRequisitionDO perfectItemRequisitionDO = null;
				//删除
				if(perfectItemRequisitionVO.getOpratorStatus().equals(PerfectItemRequisitionVO.OPRATOR_STATUS_DELETE))
				{
					if(perfectItemRequisitionVO.getKeyword()!=null){
						perfectItemRequisitionDO = findPerfectItemById(perfectItemRequisitions, perfectItemRequisitionVO.getKeyword());
						if(perfectItemRequisitionDO!=null)
						{
							perfectItemRequisitions.remove(perfectItemRequisitionDO);
						}
					}
					continue;
				}
				//新增
				if(perfectItemRequisitionVO.getOpratorStatus().equals(PerfectItemRequisitionVO.OPRATOR_STATUS_ADD))
				{
					perfectItemRequisitionDO = new PerfectItemRequisitionDO();
					perfectItemRequisitionDO = assemblePerfectItemRequisitionV2D(perfectItemRequisitionVO, perfectItemRequisitionDO,inspecteRequisiteDO);
					inspecteRequisiteDO.getPerfectItemRequisitions().add(perfectItemRequisitionDO);
				}
				//修改
				else if(perfectItemRequisitionVO.getOpratorStatus().equals(PerfectItemRequisitionVO.OPRATOR_STATUS_EDIT)){
					if(perfectItemRequisitionVO.getKeyword()!=null)
					{
						perfectItemRequisitionDO = findPerfectItemById(perfectItemRequisitions, perfectItemRequisitionVO.getKeyword());
						perfectItemRequisitionDO = assemblePerfectItemRequisitionV2D(perfectItemRequisitionVO, perfectItemRequisitionDO,inspecteRequisiteDO);
					}
				}
			}
		}

//		IdentityValidateDO identityValidateDO = new IdentityValidateDO(inspecteRequisiteVO.getIdentityValidate().getKeyword());
//		//identityValidateService.assembleIdentityValidateV2D(inspecteRequisiteVO.getIdentityValidate(), identityValidateDO);
//		inspecteRequisiteDO.setIdentityValidate(identityValidateDO);
		return inspecteRequisiteDO;
	}

	/**
	 * 功能描述: 查找整改项目 <br>
	 * @param perfectItems
	 * @param perfectItemId
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 20, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private PerfectItemRequisitionDO findPerfectItemById(Set<PerfectItemRequisitionDO> perfectItems, Long perfectItemId){
		if(perfectItems==null || perfectItems.isEmpty())
		{
			return null;
		}
		for(PerfectItemRequisitionDO perfectItemRequisitionDO : perfectItems)
		{
			if (perfectItemRequisitionDO.getKeyword()==null) {
				continue;
			}
			if(perfectItemRequisitionDO.getKeyword().longValue()==perfectItemId.longValue())
			{
				return perfectItemRequisitionDO;
			}
		}
		return null;
	}
	/**
	 * 功能描述: 整改项目 VO 转DO <br>
	 * @param inspecteRequisiteDO
	 * @param itemRequisitionVO
	 * @param itemRequisitionDO
	 * @return Create author: 王浩羽 <br>
	 *         Create on:Jan 8, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private PerfectItemRequisitionDO assemblePerfectItemRequisitionV2D(
																		PerfectItemRequisitionVO perfectItemRequisitionVO,
																		PerfectItemRequisitionDO perfectItemRequisitionDO, InspecteRequisiteDO inspecteRequisiteDO)
	{
//		BeanUtil.convertVoAsDo(perfectItemRequisitionVO, perfectItemRequisitionDO);
		String[] ignore = new String[]{"keyword", "houseNoAndOwnersMap","perfectItemTypeConfigId","perfectItemDispatchId","inspectionRequisitionId"};
		PopulateUtil.populate(perfectItemRequisitionVO, perfectItemRequisitionDO);
		perfectItemRequisitionDO.setOwners(perfectItemRequisitionVO.getOwnerName());
		perfectItemRequisitionDO.setInspectionRequisition(inspecteRequisiteDO);
		IdentityValidateDO identityValidateDO = perfectItemRequisitionDO.getInspectionRequisition().getIdentityValidate();
		if(SysrefcodeDO.OCCUPATION_IN_MANAGEHOUSE.equals(identityValidateDO.getJoinMode()))
		{
			perfectItemRequisitionDO.setHouseNumber(identityValidateService.getAllHouseNumber(identityValidateDO.getClientInfos()));
		}else{
			perfectItemRequisitionDO.setHouseNumber(perfectItemRequisitionDO.getHouseNumber());
		}
		perfectItemRequisitionDO.setOwners(perfectItemRequisitionVO.getOwnerName());
//		if(!BlankUtil.isBlank(perfectItemRequisitionVO.getPerfectItemDispatchId()))
//		{
//			perfectItemRequisitionDO.setPerfectItemDispatch(new PerfectItemDispatchDO(perfectItemRequisitionVO.getPerfectItemDispatchId()));
//		}
		perfectItemRequisitionDO.setPerfectItemTypeConfig(new PerfectItemTypeConfigDO(perfectItemRequisitionVO.getPerfectItemTypeConfigId()));
		return perfectItemRequisitionDO;
	}


	/**
	 * 功能描述: 整改项目 DO 转VO <br>
	 * @param itemRequisitionDO
	 * @param itemRequisitionVO
	 * @return Create author: 王浩羽 <br>
	 *         Create on:Jan 8, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private PerfectItemRequisitionVO assemblePerfectItemRequisitionD2V(
																		PerfectItemRequisitionDO perfectItemRequisitionDO,
																		PerfectItemRequisitionVO perfectItemRequisitionVO)
	{
		PopulateUtil.populate(perfectItemRequisitionDO, perfectItemRequisitionVO);
		perfectItemRequisitionVO.setInspectionRequisitionId(perfectItemRequisitionDO.getInspectionRequisition()
																					.getKeyword());
		perfectItemRequisitionVO.setPerfectItemTypeConfigName(perfectItemRequisitionDO.getPerfectItemTypeConfig()
																						.getPerfectItemTypeName());
		perfectItemRequisitionVO.setPerfectItemTypeConfigId(perfectItemRequisitionDO.getPerfectItemTypeConfig()
																					.getKeyword());
		perfectItemRequisitionVO.setBuildingName(perfectItemRequisitionDO.getInspectionRequisition()
																			.getIdentityValidate().getHouse()
																			.getBuilding().getName());
		// 获取完工时间
		// perfectItemRequisitionVO.setCompleteDate(perfectItemRequisitionDao.getCompleteDateById(perfectItemRequisitionDO.getKeyword()));
		SysrefcodeDO sysrefcodeDO = sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.OM_DISPATCH_STATE,
																		perfectItemRequisitionDO.getDispatchYn());
		perfectItemRequisitionVO.setDispatchYnName(sysrefcodeDO == null ? null : sysrefcodeDO.getName());
		PerfectItemDispatchDO dispatchDO = perfectItemRequisitionDO.getPerfectItemDispatch();
		perfectItemRequisitionVO.setPerfectItemDispatchId(dispatchDO == null ? null : dispatchDO.getKeyword());
//		IdentityValidateDO identityValidateDO = perfectItemRequisitionDO.getInspectionRequisition().getIdentityValidate();
//		if(SysrefcodeDO.OCCUPATION_IN_MANAGEHOUSE.equals(identityValidateDO.getJoinMode()))
//		{
//			perfectItemRequisitionVO.setHouseNumber(identityValidateService.getAllHouseNumber(identityValidateDO.getClientInfos()));
//			perfectItemRequisitionVO.setOwnerName(identityValidateService.getAllOwnerNames(identityValidateDO.getHouse().getKeyword()));
//		}else{
			perfectItemRequisitionVO.setHouseNumber(perfectItemRequisitionDO.getHouseNumber());
			perfectItemRequisitionVO.setOwnerName(perfectItemRequisitionDO.getOwners());
		//}
		return perfectItemRequisitionVO;
	}


	/**
	 * 功能描述: DO 转 VO <br>
	 * @param inspecteRequisiteDO
	 * @param inspecteRequisiteVO
	 * @return Create author: 王浩羽 <br>
	 *         Create on:Jan 5, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private InspecteRequisiteVO assembleInspecteRequisiteD2V(InspecteRequisiteDO inspecteRequisiteDO,
																InspecteRequisiteVO inspecteRequisiteVO)
	{
		String[] ignore = new String[] {"identityValidate", "perfectItemRequisitions", "meterCardinalNumbers"};
		PopulateUtil.populate(inspecteRequisiteDO, inspecteRequisiteVO, ignore);
		Set<MeterCardinalNumberDO> meterCardinalNumberDOSet = inspecteRequisiteDO.getMeterCardinalNumbers();
		for(MeterCardinalNumberDO meterCardinalNumberDO : meterCardinalNumberDOSet)
		{
			MeterCardinalNumberVO meterCardinalNumberVO = new MeterCardinalNumberVO();
			meterCardinalNumberVO = assembleMeterCardinalNumberD2V(meterCardinalNumberDO, meterCardinalNumberVO);
			inspecteRequisiteVO.getMeterCardinalNumbers().add(meterCardinalNumberVO);
		}
		Set<PerfectItemRequisitionDO> perfectItemRequisitionDOSet = inspecteRequisiteDO.getPerfectItemRequisitions();
		for(PerfectItemRequisitionDO perfectItemRequisitionDO : perfectItemRequisitionDOSet)
		{
			PerfectItemRequisitionVO perfectItemRequisitionVO = new PerfectItemRequisitionVO();
			perfectItemRequisitionVO = assemblePerfectItemRequisitionD2V(perfectItemRequisitionDO,
																			perfectItemRequisitionVO);
			inspecteRequisiteVO.getPerfectItemRequisitions().add(perfectItemRequisitionVO);
		}
		return inspecteRequisiteVO;
	}


	/**
	 * 功能描述: VO 转 DO <br>
	 * @param meterCardinalNumberVO
	 * @param meterCardinalNumberDO
	 * @param inspecteRequisiteDO
	 * @return Create author: 王浩羽 <br>
	 *         Create on:Jan 5, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private MeterCardinalNumberDO assembleMeterCardinalNumberV2D(MeterCardinalNumberVO meterCardinalNumberVO,
																	MeterCardinalNumberDO meterCardinalNumberDO,
																	InspecteRequisiteDO inspecteRequisiteDO)
	{
		if(meterCardinalNumberVO==null)
		{
			return null;
		}
		MeterCardinalNumberDO meterDO = meterCardinalNumberDO;
		if(meterDO==null)
		{
			meterDO = new MeterCardinalNumberDO();
		}
		BeanUtil.convertVoAsDo(meterCardinalNumberVO, meterDO);
		//如果是新增
		if(meterCardinalNumberVO.getCreator()!=null)
		{
			meterDO.setSysInfo(createInfo(meterCardinalNumberVO.getCreator()));
		}
		//如果是修改
		if(meterDO.getSysInfo()!=null && meterCardinalNumberVO.getModifier()!=null)
		{
			modifyInfo(meterDO.getSysInfo(), meterCardinalNumberVO.getModifier());
		}
		meterDO.setInspectionRequisition(inspecteRequisiteDO);
		return meterDO;
	}


	/**
	 * 功能描述: DO 转 VO <br>
	 * @param meterCardinalNumberDO
	 * @param meterCardinalNumberVO
	 * @return Create author: 王浩羽 <br>
	 *         Create on:Jan 5, 2009 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private MeterCardinalNumberVO assembleMeterCardinalNumberD2V(MeterCardinalNumberDO meterCardinalNumberDO,
																	MeterCardinalNumberVO meterCardinalNumberVO)
	{
		String[] ignore = new String[] {"inspectionRequisition"};
		PopulateUtil.populate(meterCardinalNumberDO, meterCardinalNumberVO, ignore);
		meterCardinalNumberVO.setInspectRequisiteId(meterCardinalNumberDO.getInspectionRequisition().getKeyword());
		return meterCardinalNumberVO;
	}


	public List<InspecteRequisiteVO> queryInspecteRequisiteByIdentityValidateId(Long identityValidateId)
	{
		List<InspecteRequisiteDO> inspecteDOs = inspecteRequisiteDao.queryInspecteRequisiteByIdentityValidateId(identityValidateId);
		if(inspecteDOs==null || inspecteDOs.isEmpty())
		{
			return null;
		}
		List<InspecteRequisiteVO> inspecteVOs = new ArrayList<InspecteRequisiteVO>();
		for(InspecteRequisiteDO inspecteDO : inspecteDOs)
		{
			InspecteRequisiteVO inspecteVO = new InspecteRequisiteVO();
			inspecteVO = assembleInspecteRequisiteD2V(inspecteDO, inspecteVO);
			inspecteVOs.add(inspecteVO);
		}
		return inspecteVOs;
	}
}
